package org.jeecg.modules.compilation.util;

import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

@Slf4j
public class WordToHtml {
    public static void main(String[] args) throws Exception {
//        docxToHtml("C:\\Users\\Lenovo\\Desktop\\新建文件夹\\19.中烟党委〔2022〕11号（中共中山市烟草专卖局机关委员会关于郑瑛同志党内职务任命的通知）.docx","C:\\Users\\Lenovo\\Desktop\\新建文件夹\\123.html");
//        docx("C:\\Users\\Lenovo\\Desktop\\新建文件夹\\中烟安〔2022〕2号.docx","C:\\Users\\Lenovo\\Desktop\\新建文件夹\\","123.htm");
        dox("C:\\Users\\Lenovo\\Desktop\\烟草局制度汇编环境部署\\Output.doc","C:\\Users\\Lenovo\\Desktop\\烟草局制度汇编环境部署\\","123.htm");
    }
    /**
     * 转换docx
     * @param filePath
     * @param htmlName
     * @throws Exception
     */
//    public static String docx(String filePath , String outPutPath ,String htmlName) throws Exception{
//        log.info("docx文件存储位置：{}，生成html路径：{}，生成html的名称：{}",filePath,outPutPath,htmlName);
//        final String file = filePath ;
//        File f = new File(file);
//        // ) 加载word文档生成 XWPFDocument对象
//        InputStream in = new FileInputStream(f);
//        XWPFDocument document = new XWPFDocument(in);
//        // ) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
//        File imageFolderFile = new File(filePath);
//        XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
//        options.setExtractor(new FileImageExtractor(imageFolderFile));
//        options.setIgnoreStylesIfUnused(false);
//        options.setFragment(true);
//        // ) 将 XWPFDocument转换成XHTML
//        OutputStream out = new FileOutputStream(new File(outPutPath + htmlName));
//        XHTMLConverter.getInstance().convert(document, out, options);
//        out.close();
//        return outPutPath + htmlName;
//    }
    /**
     * 转换doc
     * @param filePath
     * @param htmlName
     * @throws Exception
     */
    public static String dox(String filePath , String outPutPath ,String htmlName) throws Exception{
        log.info("生成html路径：{}，生成html的名称：{}",outPutPath,htmlName);
        final String file = filePath ;
        InputStream input = new FileInputStream(new File(file));
        HWPFDocument wordDocument = new HWPFDocument(input);
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        //解析word文档
        wordToHtmlConverter.processDocument(wordDocument);
        Document htmlDocument = wordToHtmlConverter.getDocument();

        File htmlFile = new File(outPutPath + htmlName);
        OutputStream outStream = new FileOutputStream(htmlFile);

        DOMSource domSource = new DOMSource(htmlDocument);
        StreamResult streamResult = new StreamResult(outStream);

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer serializer = factory.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");

        serializer.transform(domSource, streamResult);
        outStream.close();
        return outPutPath + htmlName;
    }

    /**
     * docx格式转html
     * @return
     */
    public static String  docx(String inFileName, String outFileName, String htmlName){
        String outPath = outFileName + htmlName;
        FileInputStream source = null;
        OutputStream target = null;
        try {
            // 待转换文档输入流
            source = new FileInputStream(inFileName);
            // 输出目标
            target = new FileOutputStream(outPath);
            // 待转换的docx文档对象
            XWPFDocument doc = new XWPFDocument(source);
            XHTMLOptions options = XHTMLOptions.create();
            options.setIgnoreStylesIfUnused(false);
            options.setFragment(true);
            XHTMLConverter.getInstance().convert(doc, target, options);
        } catch (Exception e){
            e.printStackTrace();
        }finally{
            try{
                //关闭资源
                if (target != null)
                    target.close();
                if(source != null)
                    source.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  outPath;
    }


}